﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using WebSocket4Net;
using System.IO;
using SuperSocket.SocketBase;
using SuperSocket.Common;
using SuperSocket.SocketEngine;
using SuperSocket.SocketEngine.Configuration;
using SuperWebSocket;

namespace TestConsole
{
    class Program
    {
        static WebSocket websocket = null;

        static void Main(string[] args)
        {

            StartSuperWebSocket();
            
        }

        private static void StartSuperWebSocket(){
        //SERVER
            var serverConfig = ConfigurationManager.GetSection("socketServer") as SocketServiceConfig;
            if (!SocketServerManager.Initialize(serverConfig))
                return;

            var socketServer = SocketServerManager.GetServerByName("SuperWebSocket") as WebSocketServer;

            socketServer.NewSessionConnected += new SessionEventHandler<WebSocketSession>(socketServer_NewSessionConnected);
            socketServer.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>(socketServer_NewMessageReceived);

            if (!SocketServerManager.Start())
                SocketServerManager.Stop();
            
            //Client
            websocket = new WebSocket("ws://localhost");
            websocket.Opened += new EventHandler(websocket_Opened);
            websocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(websocket_Error);
            websocket.Closed += new EventHandler(websocket_Closed);
            websocket.MessageReceived +=new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
            websocket.Open();
            

            string input;
            while(true)
            {
                input = Console.ReadLine();
                if (input != "exit")
                    socketServer.GetSessions(s => s.Status == SessionStatus.Healthy).ToList().ForEach(session =>
                        {
                            session.SendResponse(input);
                        });
                else
                    Environment.Exit(0);
            }
        }

        private static void websocket_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("Opened");
            websocket.Send("Hello World!");
        }

        private static void websocket_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
        {
            Console.WriteLine("Error");
        }

        private static void websocket_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("CLosed");
        }

        private static void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            Console.WriteLine("Message received");
        }

        private static void socketServer_NewSessionConnected(WebSocketSession session)
        {
            Console.WriteLine("Session connected");
        }

        private static void socketServer_NewMessageReceived(WebSocketSession session, string e)
        {
            Console.WriteLine("Server received message : {0}", e);
        }

    }
}
